package com.thirtydays.oral.common.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlConnRunner;
import cn.hutool.json.JSONObject;
import java.sql.Connection;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author Dell
 * @version 1.0
 * @date 2021/9/24 14:55
 */
@Service
@Profile({"two"})
public class DBServiceTwoImpl implements DBService {
    private static final Logger log = LoggerFactory.getLogger(DBServiceTwoImpl.class);

    public DBServiceTwoImpl() {
    }

    @Override
    public void insert(Connection connection, JSONObject parseObj) {
        log.info("开始处理新增消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String realname = parseObj.getStr("realname");
            String gender = parseObj.getStr("gender");
            String patientNo = parseObj.getStr("patientNo");
            int sex = 0;
            if ("MALE".equals(gender)) {
                sex = 1;
            }

            Long birthDate = (Long)parseObj.get("birthDate");
            LocalDateTime
                localDateTime = LocalDateTime.ofEpochSecond(birthDate / 1000L, 0, ZoneOffset.ofHours(8));
            String birthTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
            String patientId = parseObj.getStr("patientId");
            sqlConnRunner.insert(connection, Entity
                .create().setTableName("injek")
                .set("chart", patientNo)
                .set("oldchart", patientNo)
                .set("chart2", patientId)
                .set("name", realname)
                .set("name_F", realname).set("Gender", sex)
                .set("Birth", birthTime));
        } catch (Exception var11) {
            log.error("SQL错误信息: {}", var11.getMessage());
        }

    }

    @Override
    public void update(Connection connection, JSONObject parseObj) {
        log.info("开始处理修改消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String realname = parseObj.getStr("realname");
            String gender = parseObj.getStr("gender");
            Long birthDate = (Long)parseObj.get("birthDate");
            String patientId = parseObj.getStr("patientId");
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(birthDate / 1000L, 0, ZoneOffset.ofHours(8));
            String birthTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
            int sex = 0;
            if ("MALE".equals(gender)) {
                sex = 1;
            }

            sqlConnRunner.update(connection, Entity.create().setTableName("injek")
                .set("name", realname)
                .set("name_F", realname)
                .set("Gender", Integer.valueOf(sex))
                .set("Birth", birthTime), Entity.create()
                .setTableName("injek")
                .set("chart2", patientId));
        } catch (Exception var11) {
            log.error("SQL错误信息: {}", var11.getMessage());
        }

    }

    @Override
    public void delete(Connection connection, JSONObject parseObj) {
        log.info("开始处理删除消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String patientId = parseObj.getStr("patientId");
            sqlConnRunner.del(connection, Entity.create().setTableName("injek").set("chart2", patientId));
        } catch (Exception var5) {
            log.error("SQL错误信息: {}", var5.getMessage());
        }

    }
}
